﻿create procedure [dbo].[usp_AssemblyInsert]
	@CodeBase nvarchar(1024)
	, @EntryPoint nvarchar(1024)
	, @FullName nvarchar(512)
	, @GlobalAssemblyCache bit
	, @HostContext bigint
	, @ImageRuntimeVersion nvarchar(100)
	, @Location nvarchar(512)
	, @ManifestModule nvarchar(1024)
	, @ReflectionOnly bit
	, @DebugBuild bit
as

begin try
	
	begin transaction
	
		if not exists(select * from [dbo].[Assembly] where (CodeBase = @CodeBase or (CodeBase is null and @CodeBase is null)) and
			                                               (EntryPoint = @EntryPoint or (EntryPoint is null and @EntryPoint is null)) and 
				                                           FullName = @FullName and
				                                           (GlobalAssemblyCache = @GlobalAssemblyCache or (GlobalAssemblyCache is null and @GlobalAssemblyCache is null))  and
				                                           (HostContext = @HostContext or (HostContext is null and @HostContext is null)) and
				                                           ImageRuntimeVersion = @ImageRuntimeVersion and
				                                           (Location = @Location or (Location is null and @Location is null)) and
				                                           (ManifestModule = @ManifestModule or (ManifestModule is null and @ManifestModule is null)) and
				                                           (ReflectionOnly = @ReflectionOnly or (ReflectionOnly is null and @ReflectionOnly is null)) and
				                                           (DebugBuild = @DebugBuild or (DebugBuild is null and @DebugBuild is null)))
		
			begin
										                              
				insert [dbo].[Assembly]
				(
					CodeBase
					, EntryPoint
					, FullName
					, GlobalAssemblyCache
					, HostContext
					, ImageRuntimeVersion
					, Location
					, ManifestModule
					, ReflectionOnly
					, DebugBuild
				)
				values
				(
					@CodeBase
					, @EntryPoint
					, @FullName
					, @GlobalAssemblyCache
					, @HostContext
					, @ImageRuntimeVersion
					, @Location
					, @ManifestModule
					, @ReflectionOnly
					, @DebugBuild
				)
				
				select AssemblyId, ConcurrencyId, Created, Updated from [dbo].[Assembly] where @@rowcount > 0 and AssemblyId = scope_identity()
			
			end
			
		else
			
			begin
			
				select AssemblyId, ConcurrencyId, Created, Updated from [dbo].[Assembly] where (CodeBase = @CodeBase or (CodeBase is null and @CodeBase is null)) and
			                                                                                   (EntryPoint = @EntryPoint or (EntryPoint is null and @EntryPoint is null)) and 
				                                                                               FullName = @FullName and
																							   (GlobalAssemblyCache = @GlobalAssemblyCache or (GlobalAssemblyCache is null and @GlobalAssemblyCache is null))  and
																							   (HostContext = @HostContext or (HostContext is null and @HostContext is null)) and
																							   ImageRuntimeVersion = @ImageRuntimeVersion and
																							   (Location = @Location or (Location is null and @Location is null)) and
																							   (ManifestModule = @ManifestModule or (ManifestModule is null and @ManifestModule is null)) and
																							   (ReflectionOnly = @ReflectionOnly or (ReflectionOnly is null and @ReflectionOnly is null)) and
				                                                                               (DebugBuild = @DebugBuild or (DebugBuild is null and @DebugBuild is null))
			end
	
	commit transaction

end try
	
begin catch
	if @@trancount > 0
	begin
		rollback transaction;
	end
	execute [dbo].[usp_SqlErrorInsert]
end catch


